
#include "StdAfx.h"
#include "StateSet.h"
#include "StateNode.h"
#include <LogService.h>

StateSet::StateSet( char* name ){
	name_ = name;
	nodes_.clear();
	head_ = 0;
}

StateSet::~StateSet(){
}

int StateSet::register_node( char* name ){
	if(! name )
		return DEFAULT_FAIL;	

	StateNode* node = new StateNode( name );
	nodes_.push_back( node );

	if(! head_ )
		head_ = node;

	return DEFAULT_SUCCESS;
}

StateNode* StateSet::get_node( int idx ){
	if( nodes_.size() <= (size_t)idx )
		return 0;

	return nodes_[ idx ];
}

StateNode* StateSet::get_node( char* name ){
	if(! name )
		return 0;

	StateNode* node = 0;
	int size = nodes_.size();

	for( int i = 0 ; i < size ; i++ ){
		node = nodes_[ i ];

		if(! node )
			continue;

		if(! strncmp( node->get_name(), name, strlen( name ) ) )
			return node;
	}

	return 0;
}

int StateSet::size(){
	return nodes_.size();
}

int StateSet::test(){
	StateNode* p = head_;	

	while( p ){	
		LOG_INSTANCE( LOG_SERVICE( "instance" )->Log("current state is : %s", p->get_name() ) );
		p = p->process();
	}

	return DEFAULT_SUCCESS;
}
